package newapp.dob.com.dc_new_app.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public abstract class ItemAdapter<Item, Holder extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<Holder> {

    private List<Item> mSource = new ArrayList<>();
    private LayoutInflater mInflater;

    public ItemAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }


    public void update(List<Item> items) {
        mSource = items;
        notifyDataSetChanged();
    }

    public void add(List<Item> items, boolean init) {
        if (init) {
            mSource.clear();
            mSource = items;
        } else {
            mSource.addAll(items);
        }
        notifyDataSetChanged();
    }

    public void append(List<Item> items) {
        mSource.addAll(items);
        notifyDataSetChanged();
    }

    public Item getItem(int position) {
        return mSource.get(position);
    }

    public List<Item> getmSource() {
        return mSource;
    }

    protected View inflate(@LayoutRes int res, ViewGroup parent) {
        return mInflater.inflate(res, parent, false);
    }

    @Override
    public int getItemCount() {
        return mSource.size();
    }
}